/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package draw;

import delta.Calculations;

import Jama.Matrix;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JLabel;

/**
 *
 * @author Memet
 */
public class MatrixViewPane extends javax.swing.JPanel {

    int cols;
    int rows;
    Matrix matrix;
    int widthCell;
    int heightCell;
    /**
     * Creates new form MatrixViewPane
     */
    public MatrixViewPane() {
        initComponents();
        
        cols = 3;
        rows = 3;
        widthCell = 48;
        heightCell = 33;
        matrix = new Matrix(3, 3, 0);
    }

    public void sizeFix()
    {
        widthCell = (int) ((this.getSize().getWidth() - 2) / 3);
        heightCell = (int) ((this.getSize().getHeight() - 2) / 3);
    }

    public Matrix getMatrix()
    {
        return matrix;
    }

    public void setMatrix(Matrix mat)
    {
        this.matrix = mat;
        rows = mat.getRowDimension();
        cols = mat.getColumnDimension();
        this.repaint();
        updatePane();
        Matrix tijdelijk = new Matrix(3,3,0);
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                tijdelijk.set(i, j, Calculations.Round(matrix.get(i, j) / 1e9, 2));
            }
        }
        matrix = tijdelijk;
    }

    public void updatePane()
    {
        this.removeAll();
        
        NumberFormat format = new DecimalFormat("0.##E0");
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                
                JLabel lbl = new JLabel();
                lbl.setHorizontalAlignment(JLabel.CENTER);
                String txt = String.valueOf(format.format(Calculations.Round(matrix.get(i, j) / 1e9, 2)));
                String[] split = txt.split("E");
                if(split[1].equals("0"))
                {
                    txt = split[0];
                }
                
                lbl.setText(txt);
                lbl.setLocation(i*widthCell, j*heightCell);
                lbl.setSize(widthCell, heightCell);
                this.add(lbl);
                lbl.setVisible(true);
                lbl.validate();
                this.validate();
            }
        }
        
        this.validate();
        this.repaint();
    }

    public void empty()
    {
        matrix = new Matrix(3, 3, 0);
        rows = matrix.getRowDimension();
        cols = matrix.getColumnDimension();
        this.repaint();
        updatePane();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        widthCell = this.getWidth()/cols;
        heightCell = this.getHeight()/rows;
        for(int i = heightCell-1; i<this.getHeight()-heightCell; i=i+heightCell)
        {
            g2.drawLine(0, i, this.getWidth(), i);
        }
        for(int i = widthCell-1; i<this.getWidth()-widthCell; i=i+widthCell)
        {
            g2.drawLine(i, 0, i, this.getHeight());
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
